<?php

namespace app\admin\model;
use app\common\traits\Wechat;
use think\Model;
use app\common\model\Order;
use think\Db;

class Orders extends Model {
    use Wechat;
    protected $insert =['tjtime'];
    protected $type = [
        'tjtime'=>"timestamp:Y-m-d H:i:s",
    ];
    //put your code here
    public function getStatusTextAttr($value,$data)
    {
       $status = [1=>'<span class="label label-success">审核通过</span>',2=>'<span class="label label-info">审核中</span>',3=>'<span class="label label-danger">审核未通过</span>'];
       return $status[$data['status']];
    }
    public function getStatus()
    {
        return [ ['id'=>1,'name'=>'审核通过'],['id'=>2,'name'=>'审核中'],['id'=>3,'name'=>'审核未通过'] ];
    }
    public function getZhidingTextAttr($value,$data)
    {
//       $data = [['id'=>1,'name'=>'待跟进'],['id'=>2,'name'=>'邀约进店'],['id'=>3,'name'=>'邀约进店'],['id'=>4,'name'=>'未到店持续'],['id'=>5,'name'=>'未到店流失'],['id'=>6,'name'=>'成交'],['id'=>7,'name'=>'无效']];
        $status = [1=>'不指定',2=>'指定'];
        return $status[$data['zhiding']];
    }
    protected function setTjtimeAttr()
    {
        return time();
    }
    public function user(){
        return $this->belongsTo('customer','user_id','id',[],"LEFT");
    }
    //转介绍建单，通过审核
    public function pushWxToYyjl($data){
        $order = new Order();
        $info = $order->where(['id'=>$data['id'],'status'=>1])->find();
        $wxdata = $order->weixinCommonData($info);
        $wxdata['first']=$data['first'];
        $wxdata['keyword5']=$info['qdbz'];
        if(isset($data['sendid'])){//邀约经理审核通过，给自己发微信
            $wxdata['sendid'] = $data['sendid'];
        }else{//推荐自动建单，给邀约经理发微信
            $wxdata['zjs'] = 1;
            $wxdata['sendid'] = session('user_id');
        }
        $users = getYykfID();
        if($users){
            foreach ($users as $user){
                $wxdata['userid']=$user;
                $res = $this->sendWechat($wxdata);
            }
        }
        return $res;
    }
    public function pushWxToYyjlSh($id){
        $order = new Order();
        $info = Db::name('orders')->where(['id'=>$id])->find();
        $wxdata['orderid']=$id;
        $wxdata['url']=url('mobile/index/zjsshenhe',['id'=>$id],true,true);
        $wxdata['keyword1'] = $info['name'];
        $wxdata['keyword2']=$info['tel'];
        $wxdata['keyword3']="待跟进！";
        $wxdata['keyword4']='';
        $wxdata['sendid'] = session('user_id');
        $wxdata['first']="转介绍新单,需要人工审核！";
        $wxdata['keyword5']=$info['bz'];
        $wxdata['zjs'] = 1;
        $users = getYykfID();
        if($users){
            foreach ($users as $user){
                $wxdata['userid']=$user;
                $res = $this->sendWechat($wxdata);
            }
        }
        return $res;
    }
}
